/**
 * Created by maxiaoqiang on 2018/6/22.
 */
import React, {Component} from 'react';
import {Form, Icon, Input, Button, Checkbox, message} from 'antd';
import {Link} from "react-router-dom";

import md5 from "md5";

import HomeLogo from "component/homeLogo";

import {HttpClient} from "util/HttpClient";

import {history, local} from "util/MyUtil";

import "./index.less";
const FormItem = Form.Item;
class Login extends Component {


    constructor(props){
        super(props);
    }

    componentDidMount(){

    }

    handleSubmit=()=>{
        const {validateFields} = this.props.form;
        validateFields((error, values)=>{
            if (!error) {
                HttpClient.post("/v1/login", {
                    userName:values.email,
                    password:md5(values.password)
                })
                    .then((response)=>{
                        if(response.data.code==200){
                            let data = response.data.data;
                            local.set("userInfo", data);
                            history.push("/exam/userinfo")
                        }else{
                            message.error("用户名或密码错误");
                        }
                        console.log(response)
                    })
            }
        });

    }

    render() {
        const {getFieldDecorator} = this.props.form;
        return (
            <div className="user-div">
                <div className="form-div">
                    <HomeLogo/>
                    <Form onSubmit={this.handleSubmit}>
                        <FormItem hasFeedback>
                            {getFieldDecorator('email', {
                                rules: [
                                    {
                                        required: true,
                                        type: 'email',
                                        message: '请输入正确的邮箱'
                                    }
                                ]
                            })(
                                <Input
                                    prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />}
                                    placeholder="邮箱"
                                />
                            )}
                        </FormItem>
                        <FormItem hasFeedback>
                            {getFieldDecorator('password', {
                                rules: [
                                    {
                                        required: true,
                                        message: '请输入密码(6-18位)',
                                        min:6,
                                        max:18
                                    }
                                ]
                            })(
                                <Input
                                    prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />}
                                    type="password"
                                    placeholder="密码"
                                />
                            )}
                        </FormItem>
                        <FormItem>
                            <div>
                                {getFieldDecorator('remember', {
                                    valuePropName: 'checked',
                                    initialValue: true
                                })(
                                    <Checkbox>记住我</Checkbox>
                                )}
                                <Link to="/forget" className="login-form-forgot">忘记密码</Link>
                            </div>
                            <Button
                                type="primary"
                                htmlType="submit"
                                className="login-form-button"
                            >
                                登录
                            </Button>
                            <div className="login-form-login">
                                <Link to="/register">注册账户</Link>
                            </div>
                        </FormItem>
                    </Form>
                </div>
            </div>
        )
    }
}

export default Form.create()(Login);